// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "OutlookUIDemo.h"
#include "OutlookUIDemoView.h"

#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_PREVIEW_PANE, OnPreviewPane)
	ON_UPDATE_COMMAND_UI(ID_PREVIEW_PANE, OnUpdatePreviewPane)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_OUTBAR_NOTIFY, OnOutbarNotify)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	m_bPreview = TRUE;
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	// insert a flat combo box into the toolbar.
	m_pComboBox = (CCJFlatComboBox*)m_wndToolBar.InsertControl(RUNTIME_CLASS(CCJFlatComboBox), 
		NULL, CRect(0,0,200,150), ID_PLACEHOLDER, CBS_DROPDOWN );

	m_pComboBox->AddString(_T("Combo String"));
	m_pComboBox->SetCurSel(0);
	m_pComboBox->EnableAutoCompletion();

	if (!m_wndMenuBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndMenuBar.LoadMenu(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndMenuBar.EnableDocking(CBRS_ALIGN_ANY);
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndMenuBar);
	DockControlBar(&m_wndToolBar);
	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;

	cs.style = WS_OVERLAPPED|WS_CAPTION|WS_THICKFRAME|WS_SYSMENU|
		WS_MINIMIZEBOX|WS_MAXIMIZEBOX|WS_MAXIMIZE;

	cs.dwExStyle &= ~WS_EX_CLIENTEDGE;

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers


BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
{
	// Create the splitter window with two columns
	if (!m_wndSplitter.CreateStatic(this, 1, 2))
	{
		TRACE0("Failed to create splitter window\n");
		return FALSE;
	}
	
	// "Flexible pane": The second pane may present its own
	// splitter windows.
	if (!m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(COutlookUIDemoView),
		CSize(0, 0), pContext))
	{
		TRACE0("Failed to create COutlookUIDemoView\n"); 
		return FALSE;
	}

	DWORD dwStyle = 
		CGfxOutBarCtrl::fDragItems    |
		CGfxOutBarCtrl::fEditGroups   |
		CGfxOutBarCtrl::fEditItems    |
		CGfxOutBarCtrl::fRemoveGroups |
		CGfxOutBarCtrl::fRemoveItems  |
		CGfxOutBarCtrl::fAddGroups    |
		CGfxOutBarCtrl::fAnimation;
	
	// Here we create the outbar control; we give as id the parent splitter pane id here
	if (!m_wndOutlookBar.Create(WS_CHILD|WS_VISIBLE, CRect(0,0,0,0),
		&m_wndSplitter, m_wndSplitter.IdFromRowCol(0, 0), dwStyle))
	{
		TRACE0("Failed to create outlook bar.");
		return FALSE;
	}
    
	// Tell the control to send message to this window (the mainframe) 
	// and not to its real parent (the splitter)
	m_wndOutlookBar.SetOwner(this);
	
	// Here we create the imagelists for the control
	m_ImageSmall.Create (16, 16, TRUE, 2, 1);
	m_ImageLarge.Create (32, 32, TRUE, 2, 1);

	for( int nIcon = IDI_ICON_OUTLOOK; nIcon <= IDI_ICON_PUBLIC; ++nIcon ) 
	{
		HICON hIcon = AfxGetApp()->LoadIcon(nIcon);
		ASSERT(hIcon);

		m_ImageSmall.Add(hIcon);
		m_ImageLarge.Add(hIcon);
	}

	// and we link them to the control
	m_wndOutlookBar.SetImageList(&m_ImageLarge, CGfxOutBarCtrl::fLargeIcon);
	m_wndOutlookBar.SetImageList(&m_ImageSmall, CGfxOutBarCtrl::fSmallIcon);

	m_wndOutlookBar.SetAnimationTickCount(20);
	m_wndOutlookBar.SetAnimSelHighlight(200);

	// Look at function reference for information about linking image list
	// Here we can add the folders to the control; we need at least one folder.
	// The numbers aside the text are an "lParam" value we can assign to each folder
	m_wndOutlookBar.AddFolder(_T("Outlook Shortcuts"),  FOLDER_0);
	m_wndOutlookBar.AddFolder(_T("My Shortcuts"),		FOLDER_1);

	// Here we insert the items; syntax is folder, index, text, image, lParam value for item
	m_wndOutlookBar.InsertItem(FOLDER_0, CMD_00, _T("Outlook Today"),  0, 0);
	m_wndOutlookBar.InsertItem(FOLDER_0, CMD_01, _T("Inbox"),		   1, 0);
	m_wndOutlookBar.InsertItem(FOLDER_0, CMD_02, _T("Calendar"),	   2, 0);
	m_wndOutlookBar.InsertItem(FOLDER_0, CMD_03, _T("Contacts"),	   3, 0);
	m_wndOutlookBar.InsertItem(FOLDER_0, CMD_04, _T("Tasks"),		   4, 0);
	m_wndOutlookBar.InsertItem(FOLDER_0, CMD_05, _T("Journal"),		   5, 0);
	m_wndOutlookBar.InsertItem(FOLDER_0, CMD_06, _T("Notes"),	       6, 0);
	m_wndOutlookBar.InsertItem(FOLDER_0, CMD_07, _T("Deleted Items"),  7, 0);
	
	m_wndOutlookBar.InsertItem(FOLDER_1, CMD_00, _T("Drafts"),		   8, 0);
	m_wndOutlookBar.InsertItem(FOLDER_1, CMD_01, _T("Outbox"),		   9, 0);
	m_wndOutlookBar.InsertItem(FOLDER_1, CMD_02, _T("Sent Items"),	  10, 0);
	
	m_wndOutlookBar.SetSelFolder(0);

	// Standard sizing for splitter
	CRect r;
	GetClientRect(&r);
	int w1 = r.Width()/7;
	int w2 = r.Width()/5;
	m_wndSplitter.SetColumnInfo( 0, w1, 0 );
	m_wndSplitter.SetColumnInfo( 1, w2, 0 );
	m_wndSplitter.RecalcLayout();

	return TRUE;
}

long CMainFrame::OnOutbarNotify(WPARAM wParam, LPARAM lParam)
{
	switch (wParam)
	{
	case NM_OB_ITEMCLICK:
		// cast the lParam to an integer to get the clicked item
		{
			// Get the current folder.
			int nFolder = m_wndOutlookBar.GetSelFolder();
			
			// Get the current selection.
			int nIndex  = (int)lParam;
			CCJTreeCtrl* pTreeCtrl = GetMainView()->GetFolderTreeCtrl();
			HTREEITEM htiFound = NULL;
			HTREEITEM hti = pTreeCtrl->GetSelectedItem();

			switch (nFolder)
			{
			case FOLDER_0: // Information folder is selected...
				{
					switch (nIndex)
					{
					case CMD_00:
						htiFound = pTreeCtrl->FindItem(_T("Outlook Today"));
						break;
					case CMD_01:
						htiFound = pTreeCtrl->FindItem(_T("Inbox"));
						break;
					case CMD_02:
						htiFound = pTreeCtrl->FindItem(_T("Calendar"));
						break;
					case CMD_03:
						htiFound = pTreeCtrl->FindItem(_T("Contacts"));
						break;
					case CMD_04:
						htiFound = pTreeCtrl->FindItem(_T("Tasks"));
						break;
					case CMD_05:
						htiFound = pTreeCtrl->FindItem(_T("Journal"));
						break;
					case CMD_06:
						htiFound = pTreeCtrl->FindItem(_T("Notes"));
						break;
					case CMD_07:
						htiFound = pTreeCtrl->FindItem(_T("Deleted Items"));
						break;
					}
				}
				break;
				
			case FOLDER_1: // Management folder is selected...
				{
					switch (nIndex)
					{
					case CMD_00:
						htiFound = pTreeCtrl->FindItem(_T("Drafts"));
						break;
					case CMD_01:
						htiFound = pTreeCtrl->FindItem(_T("Outbox"));
						break;
					case CMD_02:
						htiFound = pTreeCtrl->FindItem(_T("Sent Items"));
						break;
					}
				}
				break;
			}

			// select the new tree item.
			if(hti != htiFound) {
				pTreeCtrl->SelectItem(htiFound);
			}
		}
		
		return 0;

	case NM_OB_ONLABELENDEDIT:
		// cast the lParam to an OUTBAR_INFO * struct; it will contain info about the edited item
		// return 1 to do the change and 0 to cancel it
		{
			OUTBAR_INFO * pOI = (OUTBAR_INFO *) lParam;
			TRACE2("Editing item %d, new text:%s\n", pOI->index, pOI->cText);
		}
		return 1;
		
	case NM_OB_ONGROUPENDEDIT:
		// cast the lParam to an OUTBAR_INFO * struct; it will contain info about the edited folder
		// return 1 to do the change and 0 to cancel it
		{
			OUTBAR_INFO * pOI = (OUTBAR_INFO *) lParam;
			TRACE2("Editing folder %d, new text:%s\n", pOI->index, pOI->cText);
		}
		return 1;
		
	case NM_OB_DRAGITEM:
		// cast the lParam to an OUTBAR_INFO * struct; it will contain info about the dragged items
		// return 1 to do the change and 0 to cancel it
		{
			OUTBAR_INFO * pOI = (OUTBAR_INFO *) lParam;
			TRACE2("Drag item %d at position %d\n", pOI->iDragFrom, pOI->iDragTo);
		}
		return 1;
	}
	return 0;
}

void CMainFrame::OnGfxLargeicon() 
{
	m_wndOutlookBar.SetSmallIconView(false);
}

void CMainFrame::OnUpdateGfxLargeicon(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(!m_wndOutlookBar.IsSmallIconView());
}

void CMainFrame::OnGfxSmallicon() 
{
	m_wndOutlookBar.SetSmallIconView(true);
}

void CMainFrame::OnUpdateGfxSmallicon(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(m_wndOutlookBar.IsSmallIconView());
}

COutlookUIDemoView* CMainFrame::GetMainView()
{
	COutlookUIDemoView* pView = DYNAMIC_DOWNCAST(COutlookUIDemoView, m_wndSplitter.GetPane(0,1));
	ASSERT_KINDOF(COutlookUIDemoView, pView);
	return pView;
}

void CMainFrame::OnPreviewPane() 
{
	if(m_bPreview) {
		GetMainView()->m_wndSplitter2.HideRow(1);
	}
	else {
		GetMainView()->m_wndSplitter2.ShowRow();
	}
	m_bPreview = !m_bPreview;
}

void CMainFrame::OnUpdatePreviewPane(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(m_bPreview);
}
